﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class DikeSettleOffsetReposity : Reposity
    {
        public stack_settle_offset GetDikeSettleInfo(int stackId, DateTime date)
        {
            DateTime monthDate = new DateTime(date.Year, date.Month, 1);
            var dikeSettleInfo = from s in _dataContext.stack_settle_offsets
                                 where stackId == s.stack_id && s.date == monthDate.Date
                             select s;

            if (dikeSettleInfo != null && dikeSettleInfo.Count() > 0)
            {
                return dikeSettleInfo.First();
            }
            else
            {
                return null;
            }
        }

        public stack_settle_offset CreateDikeSettleInfo(stack_settle_offset dikeSettleInfo)
        {
            _dataContext.stack_settle_offsets.InsertOnSubmit(dikeSettleInfo);
            SubmitChanges();
            return dikeSettleInfo;
        }

        public bool DeleteDikeSettleInfo(stack_settle_offset dikeSettleInfo)
        {
            _dataContext.stack_settle_offsets.DeleteOnSubmit(dikeSettleInfo);
            return true;
        }

        public IEnumerable<stack_settle_offset_pool_info> GetMonthSettleOffsetPoolInfo(int stackId, DateTime date)
        {
            DateTime monthDate = new DateTime(date.Year, date.Month, 1);
            var pools = from s in _dataContext.stack_settle_offset_pool_infos
                              where stackId == s.stack_id && s.date == monthDate.Date
                              select s;

            foreach (stack_settle_offset_pool_info pool in pools)
            {
                yield return pool;
            }
        }

        public stack_settle_offset_pool_info CreateSettleOffsetPoolInfo(stack_settle_offset_pool_info poolInfo)
        {
            _dataContext.stack_settle_offset_pool_infos.InsertOnSubmit(poolInfo);
            SubmitChanges();
            return poolInfo;
        }

        public void DeleteSettleOffsetPoolInfos(int stackId, DateTime date)
        {
            DateTime monthDate = new DateTime(date.Year, date.Month, 1);
            var poolInfos = from s in _dataContext.stack_settle_offset_pool_infos
                            where s.stack_id == stackId && s.date == monthDate.Date
                                      select s;

            foreach (stack_settle_offset_pool_info pool in poolInfos)
            {
                _dataContext.stack_settle_offset_pool_infos.DeleteOnSubmit(pool);
            }
            SubmitChanges();
        }
    }
}